<template>
    <div>
        <headerSearch title="新闻列表" :searchBack="true" @done="gotobtn"></headerSearch>
        <rearchKey :search_intop="search_intop" :searchISshow="searchISshow" v-on:spot="spot" :clearShow="clearShow" @clearClick="clearClick" @searchCancel="searchCancel"></rearchKey>
        <div class="pagecontent main-newlist" style="background-color: #fff;padding: 0 0.2rem;">
            <!-- 新闻列表 -->
            <div class="new_info_list">
                <!-- <div style="font-size: 0.3rem; color: rgb(102, 102, 102);     line-height: 0.5rem;height: 0.5rem;" v-if="infos == ''">
                --&nbsp;暂无数据&nbsp;--
                </div> -->
                <ul v-for="(initem, t) in infos" :key="t">
                    <li @click="newsdetailClick(initem.IDNews)" v-if="initem.count ==1" class="new_info_list_li">
                        <a href="javascript:void(0)" class="clear">
                        <div class="new_info_single_img fl">
                            <img src="../common/images/cindex_41.jpg"  v-if="initem.img == null">
                            <img :src="initem.img" alt="" v-else> 
                        </div>
                        <p class="new_info_itemTitle">{{initem.Title}}</p>
                        <p class="new_info_itemdesc clear"><span class="fl">{{initem.summary}}</span><span class="fr">{{initem.dtCreate}}</span></p>
                        </a>
                    </li>
                    <li @click="newsdetailClick(initem.IDNews)" v-else>
                        <div class="new_info_item">
                        <p>{{initem.Title}}</p>
                        <ul class="clear">
                            <li class="fl" v-for="(info, fo) in initem.img" :key="fo">
                            <a href="javascript:void(0)">
                                <span class="new_info_img fl">
                                  <img src="../common/images/cindex_41.jpg"  v-if="info == null">
                                  <img :src="info" alt="" v-else>
                                </span>										
                            </a>
                            </li>
                        </ul>
                        <p class="new_info_desc clear"><span class="fl">{{initem.summary}}</span><span class="fr">{{initem.dtCreate}}</span></p>
                        </div> 
                    </li>
                </ul>

              <infinite-loading @infinite="infiniteHandler">
                  <span slot="no-more">
                  (ฅ>ω<*ฅ)我们是有底线的(ฅ>ω<*ฅ)
                  </span>
                  <span slot="no-results" class="contentEmpty" style="margin-top: 0.6rem;">
                      <template v-if="infos.length">
                          (ฅ>ω<*ฅ)我们是有底线的(ฅ>ω<*ฅ)
                      </template>
                      <template v-if="!infos.length">
                        <img src="../common/images/failedLoad.png" alt="">
                        <em>暂无查询结果</em>
                      </template>
                  </span>
              </infinite-loading>
            </div>
        </div>
    </div>
</template>

<script>
import headerSearch from '../public/header-search.vue';
import rearchKey from '../public/rearchKey';
import InfiniteLoading from 'vue-infinite-loading';
    export default {
        data() {
            return {
                // path:'http://fc.x.e000.com',
                // infos: [
                //     {id:1},{id:2},{id:3}
                // ],
                infolist: [
                    {
                        info_name: '从零开始学习健身，第15天，参照第一天的样子，照第一天的样子。',
                        into_company: '北京朝阳区块链有限公司',
                        info_time: '2018-11-21'
                    },
                    {
                        info_name: '从零开始学习健身，第15天，参照第一天的样子，照第一天的样子。',
                        into_company: '北京朝阳区块链有限公司',
                        info_time: '2018-11-21'
                    },
                    {
                        info_name: '从零开始学习健身，第15天，参照第一天的样子，照第一天的样子。',
                        into_company: '北京朝阳区块链有限公司',
                        info_time: '2018-11-21'
                    },
                ],
                search_intop: '',      //遮罩层样式
                // search: '',            //输入内容
                searchISshow: false,  
                clearShow: false,     //清除内容
                items: [],
                page:0,
                num:10,
                cpId:'',
                keyword:'0',
            }
        },
        watch: {
            keyword(value) {
              console.log('获取input值', value);
                if(value != '') {
                  this.clearShow = true;
                  this.search_intop = 'background-color: transparent; z-index: 0;';
                }else {
                  this.clearShow = false;
                  this.search_intop = 'background-color: rgba(0, 0, 0, 0.5);';
                }
            }
        },
        components: {
            headerSearch,
            rearchKey,
            InfiniteLoading
        },
        mounted() {
          //获取所有的新闻信息
          this.cpId = this.$route.query.cpId;
          this.getAllNewsList();
        },
        methods: {
            spot(value) {
                this.keyword = value;
                console.log('获取input值999', value);
            },
            infiniteHandler($state) {
                let that = this;
                let page = that.page;
                let keyword = that.keyword;
                let cpId = that.cpId;
                let type = 1;
                console.log("触发")
                setTimeout(() => {       
                    const temp = []; 
                    that.$fetch(that.$path+'/index.php/getAllNewsList',{page,keyword,cpId,type})
                        .then((res) => {
                        console.log('首页',res);
                        if(res.code == 200){
                        let array = res.data;
                        that.page++;
                        if(that.num > array.length){
                            $state.complete();
                            return;
                        }
                        array.forEach(element => {
                            temp.push(element);
                        });
                        that.items = that.items.concat(temp);  
                        $state.loaded();  
                        }else{
                            $state.complete();
                            return;
                        }    
                    });  
                }, 1000);
          },
          //获取所有的新闻信息
          getAllNewsList(){
            let that = this;
            let page = that.page;
            let keyword = that.keyword;
            let cpId = that.cpId;
            let type = 1;
            that.$fetch(that.$path+'/index.php/getAllNewsList',{page,keyword,cpId,type})
                .then((res) => {
                  console.log('首页',res);
                if(res.code == 200){
                  that.items = res.data
                  // that.actives = res.data.a_list;
                  // that.infos = res.data.n_list;
                  
                
                }    
            });
          },
          //点击新闻详情
            newsdetailClick(val) {
                this.$router.push({ path: '/newsDetail', query: {id: val}});
            },
          // 点击搜索图标出现搜索框
          gotobtn() {
            console.log('点击')
            this.searchISshow = true;
          },
          // 点击取消隐藏搜索框
          searchCancel(data) {
            // console.log('11212121',this.keyword)
            this.searchISshow = false;
            data = '';
          },
          // 清除内容
          clearClick(data) {
            this.clearShow = false;
            data = '';
          },
        },
        computed: {
            //过滤方法
          infos: function() {
            let that = this;
              var _search = that.keyword;
              console.log()
              if (_search) {
                  //不区分大小写处理
                  var reg = new RegExp(_search, 'ig')
                  //es6 filter过滤匹配，有则返回当前，无则返回所有
                  return that.items.filter(function(e) {
                      //匹配所有字段
                      return Object.keys(e).some(function(key) {
                          let data = e.Title + e.summary + e.dtCreate;
                          return data.match(reg);
                      })
                      //匹配某个字段
                      //  return e.name.match(reg);
                  })
              };
              return that.items;
          }
        },  
    }
</script>

<style>
@import '../common/css/index.css';

.main-newlist {
    background-color: #fff;
}
/* 新闻 */
/* 多图 */
.new_info_list {
  margin: 0 -0.2rem;
}
.new_info_list .new_info_item {
  padding: 0 0.2rem;
  border-bottom: 1px solid #d8d8d8;
}
.new_info_list .new_info_item p {
  font-size: 0.28rem;
  color: #333;
  text-align: left;
  line-height: 0.46rem;
}
.new_info_list .new_info_item .new_info_img {
  width: 2.24rem;
  height: 1.46rem;
  margin-right: 0.19rem;
}
.new_info_list .new_info_item li:last-child .new_info_img {
  margin-right: 0;
}
.new_info_list .new_info_item .new_info_img img {
  width: 100%;
  height: 1.46rem;
}
.new_info_list .new_info_item .new_info_desc {
  line-height: 0.36rem;
  font-size: 0.22rem;
  color: #999;
  line-height: 0.46rem;
}

/* 单个图 */
.new_info_list .new_info_list_li {
  border-bottom: 1px solid #d8d8d8;
  padding: 0.2rem;
  position: relative;
}
.new_info_list .new_info_list_li .new_info_single_img {
  width: 1.2rem;
  height: 1.2rem;
  margin-right: 0.2rem;
  text-align: center;
  vertical-align: middle;
  display: block;
}
.new_info_list .new_info_list_li .new_info_single_img img {
  width: 1.2rem;
  height: 1.2rem;
  max-width: 100%;
  max-height: 100%;
}
.new_info_list .new_info_list_li .new_info_itemTitle {
  font-size: 0.28rem;
  color: #333;
  text-align: left;
  text-overflow: ellipsis;
  -o-text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-height: 0.36rem;
}
.new_info_list .new_info_list_li .new_info_itemdesc {
  font-size: 0.22rem;
  color: #999;
  text-align: left;
  position: absolute;
  right: 0.2rem;
  bottom: 0.2rem;
  width: 76%;
  line-height: 0.3rem;
}
/* 新闻右边详情 */
.new_info_list .new_info_list_li .new_info_itemdesc span:first-child {
    width: 2rem;
    white-space: nowrap;
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;
    overflow: hidden;
}

</style>